<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文创名称">
        <el-input v-model="queryParams.caciName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.caciPutaway" placeholder="请选择" clearable>
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          placeholder="全部"
          v-model="queryParams.date"
          size="small"
          value-format="yyyy-MM-dd"
          type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-button style="float: right;"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
    </el-form>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="封面图" align="center" prop="noticeType" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.caciCoverImg" style="width: 100px; height: 100px;" />
        </template>
      </el-table-column>
      <el-table-column label="文创名称" align="center" prop="caciName" width="200"></el-table-column>
      <el-table-column label="状态" align="center" prop="caciPuaway" width="100"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span @click="handleDelete(scope.row)" class="red">删除</span>
          <span v-if="scope.row.status==1" @click="handleLang(scope.row)" class="sanm">上架</span>
          <span v-else @click="handleLang(scope.row)" class="sanm">下架</span>
          <span @click="handleLang(scope.row)" class="sanm">多语言配置</span>
          <span @click="handleUpdate(scope.row)" class="sanm">编辑</span>
          <span @click="handleView(scope.row)" class="sanm">详情</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="封面图片" prop="caciCoverImg">
              <input type="hidden" v-model="form.caciCoverImg" />
              <img v-if="form.caciCoverImg" :src="form.caciCoverImg" style="width:150px;" />
              <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
                <el-button size="small">
                  上传图片
                  <i class="el-icon-upload el-icon--right"></i>
                </el-button>
              </el-upload>
              <div class="tip">仅支持jpg、png、jpeg格式</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文创名称" prop="caciName">
              <el-input v-model="form.caciName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="直接上架" prop="caciPuaway">
              <el-radio-group v-model="form.caciPuaway">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="产品展示" prop="noticeType">

              <el-upload
                action="#"
                list-type="picture-card"
                :http-request="requestUpload2" :show-file-list="true" :before-upload="beforeUpload2"
                :file-list="caciShow"
                >
                <i class="el-icon-plus"></i>
              </el-upload>
<!-- 
              <img v-for="(item, index) in" :src="form.caciImg" style="width:150px;" />
              <el-upload action="#" :http-request="requestUpload2" :show-file-list="false" :before-upload="beforeUpload2">
                <el-button size="small">
                  上传内容
                  <i class="el-icon-upload el-icon--right"></i>
                </el-button>
              </el-upload> -->
              <div class="tip">图片仅支持jpg、png、jpeg格式，视频仅支持mp4、flv、m3u8格式</div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="产品介绍">
              <editor v-model="form.caciIntroduce" :min-height="192"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="文创详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form ref="form" label-width="150px">
          <el-form-item label="封面图片">
            <img :src="form.caciCoverImg" style="width:150px;" />
          </el-form-item>
          <el-form-item label="文创名称">
            <span>{{form.caciName}}</span>
          </el-form-item>
          <el-form-item label="直接上架">
            <span>{{form.caciPuaway}}</span>
          </el-form-item>
          <el-form-item label="产品介绍">
            <span>{{form.caciIntroduce}}</span>
          </el-form-item>
          <el-form-item label="产品展示">
            <img v-for="(item, index) in caciShow" :key="index" :src="item" style="height:100px; margin: 0 5px 5px 0;" />
          </el-form-item>

      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { upload, removeFile } from '@/api/common';
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/make";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caciImg: [
          { required: true, message: "请上传封面", trigger: "blur" }
        ],
        caciName: [
          { required: true, message: "请输入文创名称", trigger: "blur" }
        ]
      },
      daterangeCreateTime: [],
      caciShow: [],

      viewOpen: false,

      //多语言设置
      langOpen: false,
      langTit: '多语言配置',
      //type 1输入框 2图片上传 3富文本
      langDicts: [
        {id: 2, name: '名称', status: 0, flag: 1, type: 1, val: 'http://www.baidu.com'}
      ],
      setCur: {},
      setOpen: false,
      langs: [
        {id: 1, name: '中文（简体）', val: ''},
        {id: 2, name: '中文（繁体）', val: ''},
        {id: 3, name: '英语（English）', val: ''}
      ]

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data.list || response.data || [];
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    handleLang(row){
      this.$refs.lang.init(row, 'CACI','文创');
    },
    handleSet(row){
      this.setCur = row;
      this.setOpen = true;
    },
    setSubmit(){
      addNotice(this.form).then(response => {
        this.$modal.msgSuccess("设置成功");
        this.setOpen = false;
        this.getList();
      });
    },
    setCancel(){
      this.setOpen = false;
    },
    handleView(row){
      this.form = row;
      this.viewOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        caciCoverImg: ''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增文创";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

        this.form = row;
        this.open = true;
        this.title = "编辑文创";

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let caciShow = this.caciShow;
          let arr = [];
          caciShow.forEach(item=>{
            arr.push(item.url)
          })
          this.form.caciShow = arr.join(",");
          if (this.form.id != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 覆盖默认的上传行为
    requestUpload(e) {
      let formData = new FormData();
      formData.append('file', e.file);
      formData.append('path', 'user/info');
      upload(formData).then(res => {
        if(res.code=='00000'){
          this.form.caciCoverImg = res.data.filePath;
          this.$modal.msgSuccess("上传成功");
        }
        else{
          this.$modal.msgError("上传失败");
        }
      });
    },
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
        return false
      } else {

      }
    },
    requestUpload2(e) {
      let formData = new FormData();
      formData.append('file', e.file);
      formData.append('path', 'user/info');
      upload(formData).then(res => {
        if(res.code=='00000'){
          this.caciShow.push({
            name: res.data.fileName,
            url: res.data.filePath
          });
          this.$modal.msgSuccess("上传成功");
        }
        else{
          this.$modal.msgError("上传失败");
        }
      });
    },
    // 上传预处理
    beforeUpload2(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
        return false
      } else {

      }
    }
  }
};
</script>